package com.wlz.demo.controller;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.wlz.demo.common.ResultUtil;
import com.wlz.demo.controller.vo.BarVO;
import com.wlz.demo.service.IHobbyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * (Hobby)表控制层
 * @since 2022-04-11 16:49:13
 */
@RestController
@RequestMapping("hobby")
public class HobbyController {

    @Autowired(required = true)
    private IHobbyService iHobbyService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    public static final String Echart_Data_KEY = "Echart_Data_KEY";

//    @PostMapping("/save")
//
//    public ResultUtil<?> save(@RequestBody Hobby hobby){
//        boolean save = iHobbyService.save(hobby);
//        if (save){
//            return ResultUtil.success(200,"新增成功");
//        }else {
//            return ResultUtil.fail(500,"新增失败");
//        }
//    }

//    @GetMapping("/list")
//    public ResultUtil<?> list(){
//        List<Hobby> list = iHobbyService.list(null);
//        if (list.size()!=0){
//            return ResultUtil.success(200,"查询成功",list);
//        }else {
//            return ResultUtil.fail(500,"查询失败");
//        }
//    }

//    @DeleteMapping("/delete")
////    @CacheEvict(value = "hobby",key = "'getAll'")
//    public ResultUtil<?> delete(@PathParam(value = "id") Integer id){
//        boolean b = iHobbyService.removeById(id);
//        if (b){
//            List<Hobby> list = iHobbyService.list(null);
//            return ResultUtil.success(200,"删除成功",list);
//        }else {
//            return ResultUtil.fail(500,"删除失败");
//        }
//
//    }

//    @PutMapping("/updateById")
//
//    public ResultUtil<?> updateById(@RequestBody Hobby hobby){
//        boolean b = iHobbyService.updateById(hobby);
//        if (b){
//            Hobby one = iHobbyService.getById(hobby.getHobbyId());
//            return ResultUtil.success(200,"修改成功",one);
//        }else {
//            return ResultUtil.fail(500,"修改失败");
//        }
//    }

//    //多个模糊查询+分页
//    @GetMapping("/page")
//    //cache缓存
////    @Cacheable(value = "hobby",key = "#pageNum+'-'+#pageSize+'-'+#hobbyname+'-'+#phone+'-'+#address")
////    @Cacheable(value = "hobby",key = "'getAll'")
//    public IPage<Hobby> pageLike(@RequestParam Integer pageNum,
//                                @RequestParam Integer pageSize,
//                                @RequestParam(required=false) String hobbyName
//    ){
//        LambdaQueryWrapper<Hobby> lqw = new LambdaQueryWrapper<>();
//        lqw.like(hobbyName!=null,Hobby::getHobbyName,hobbyName);
//        IPage<Hobby> iPage = new Page<>(pageNum,pageSize);
//                IPage<Hobby> page = iHobbyService.page(iPage,lqw);
//                return page;
//    }

   



//    //通过id查个人信息
//    @GetMapping("/getOneByHobbyname/{hobbyname}")
//    public ResultUtil<?> getById(@PathVariable String hobbyName){
//        LambdaQueryWrapper<Hobby> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(Hobby::getHobbyName,hobbyName);
//        Hobby one = iHobbyService.getOne(lqw);
//        if ( one != null ){
//            return ResultUtil.success(200,"查询成功",one);
//        }else {
//            return ResultUtil.fail(401,"查询失败");
//        }
//    }


    //echart
    @CrossOrigin
    @GetMapping("/echart1")
    public ResultUtil<?> barVO() {
        //缓存步骤1：从缓存获取数据
        String jsonStr = stringRedisTemplate.opsForValue().get(Echart_Data_KEY);
        BarVO barvo;
        //缓存步骤2：如果json是空的 就set到redis中
        if (StrUtil.isBlank(jsonStr)){
            barvo = iHobbyService.barvo();
            stringRedisTemplate.opsForValue().set(Echart_Data_KEY, JSONUtil.toJsonStr(barvo));
        }else {
            //缓存步骤3：如果redis有则直接从缓存中拿数据
            barvo = JSONUtil.toBean(jsonStr, new TypeReference<BarVO>() {
            },true);
        }

        return ResultUtil.success(barvo);
    }



}

